﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.Mvc;
using System.Web.UI.WebControls;
using Eff.Data.Sql;
using Eff.Extensions;
using Redmine.Core.UnitOfWork;
using UnitOfWork;

namespace Redmine.Web.Controllers
{
    public class BaseController : Controller
    {
        protected readonly IUnitOfWork _unitOfWork;

        public BaseController(IUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }

        public ActionResult CheckUniqueness(string tn)
        {
            bool exists;
            if (Request.QueryString.AllKeys.Length != 2)
                throw new Exception("非法操作");
            var fieldName = Request.QueryString.AllKeys[1].Replace(" ", ""); //todo sql注入
            var value = Request.QueryString[fieldName].Replace(" ", "");

            using (var helper = new SqlHelper(DbDataContext.ConStrKey))
            {
                var sqlParam = new SqlParameter("value", value);

                exists = helper.ExecuteScalar(string.Format("select 1 from {0} where {1}=@value", tn, fieldName), CommandType.Text, sqlParam).AsInt() == 1;
            }
            return Json(!exists, JsonRequestBehavior.AllowGet);
        }
    }
}
